package com.ruoyi.system.config;


import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class RabMqConfig {


    //交换机
    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange("fanoutExchange",true,false);
    }
    //队列
    @Bean
    public Queue queue1(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("x-dead-letter-exchange", "deadExchange");
        map.put("x-dead-letter-routing-key", "deadkey1");
        map.put("x-message-ttl", 10000);
        return QueueBuilder.durable("queue1").withArguments(map).build();
    }

    //交换机和队列绑定
    @Bean
    public Binding binding1(@Qualifier("queue1")Queue queue, @Qualifier("fanoutExchange")FanoutExchange fanoutExchange){
        return BindingBuilder.bind(queue).to(fanoutExchange);
    }

    //主题
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange("topicExchange",true,false);
    }
    //主题队列
    @Bean
    public Queue queue2(){
        return QueueBuilder.durable("queue2").build();
    }
    @Bean
    public Queue queue3(){
        return QueueBuilder.durable("queue3").build();
    }
    @Bean
    public Queue queue4(){
        return QueueBuilder.durable("queue4").build();
    }
    //绑定交换机和队列（topic）
    @Bean
    public Binding bandingtop1(@Qualifier("queue2")Queue queue, @Qualifier("topicExchange")TopicExchange topicExchange){
        return BindingBuilder.bind(queue).to(topicExchange).with("topic.first");
    }
    @Bean
    public Binding bandingtop2(@Qualifier("queue3")Queue queue, @Qualifier("topicExchange")TopicExchange topicExchange){
        return BindingBuilder.bind(queue).to(topicExchange).with("topic.two");
    }
    @Bean
    public Binding bandingtop3(@Qualifier("queue4")Queue queue, @Qualifier("topicExchange")TopicExchange topicExchange){
        return BindingBuilder.bind(queue).to(topicExchange).with("topic.three");
    }




    //================================死信======================================//
    //死信交换机
    @Bean("deadExchange")
    public DirectExchange directExchange(){
        return new DirectExchange("deadExchange");
    }
    //死信队列
    @Bean("deadqueue1")
    public Queue deadqueue1(){
        return new Queue("deadqueue1");
    }

    //绑定死信交换机和死信队列
    @Bean
    public Binding bindingdead(@Qualifier("deadqueue1")Queue queue,@Qualifier("deadExchange")DirectExchange directExchange){
        return BindingBuilder.bind(queue).to(directExchange).with("deadkey1");
    }



}
